/*
 * Copyright 2008-2010 Daniel Cachapa <cachapa@gmail.com>
 * 
 * This program is distributed under the terms of the GNU General Public License Version 3
 * The license can be read in its entirety in the LICENSE.txt file accompanying this source code,
 * or at: http://www.gnu.org/copyleft/gpl.html
 * 
 * This file is part of Libra.
 *
 * WeightWatch is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3 of the License.
 *
 * WeightWatch is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the WeightWatch source code. If not, see: http://www.gnu.org/licenses
 */

package net.cachapa.libra.dailyburn;

import java.io.StringReader;
import java.util.Vector;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;

import net.cachapa.libra.data.Value;
import net.cachapa.libra.util.MyDate;

public class XmlParser {

	public static Vector<Value> parseBodyLogEntries(String xml) {
		Vector<Value> values = new Vector<Value>();
		try {
			XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser();
			xpp.setInput(new StringReader(xml));
			int eventType = xpp.getEventType();
			Value value;
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					if (xpp.getName().equals("body-log-entry")) {
						value = parseBodyLogEntry(xpp);
						if (value != null) {
							values.add(value);
						}
					}
				}
				eventType = xpp.next();
			}

		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}
		return values;
	}
	
	private static Value parseBodyLogEntry(XmlPullParser xpp) {
		String tagName;
		MyDate date = null;
		float weight = 0;
		try {
			int eventType = xpp.getEventType();
			while (!(eventType == XmlPullParser.END_TAG && xpp.getName().equals("body-log-entry"))) {
				if (eventType == XmlPullParser.START_TAG) {
					tagName = xpp.getName();
					if (tagName.equals("body-metric-id")) {
						if (!xpp.nextText().equals("8")) {
							return null;
						}
					}
					else if (tagName.equals("logged-on")) {
						date = new MyDate(new Integer(xpp.nextText().replace("-", "")).intValue());
					}
					else if (tagName.equals("value")) {
						weight = new Float(xpp.nextText()).floatValue();
					}
				}
				eventType = xpp.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return new Value(date, weight, 0f);
	}

	public static String parseUserDetails(String xml) {
		String username = null;
		try {
			XmlPullParser xpp = XmlPullParserFactory.newInstance().newPullParser();
			xpp.setInput(new StringReader(xml));
			int eventType = xpp.getEventType();
			while (eventType != XmlPullParser.END_DOCUMENT) {
				if (eventType == XmlPullParser.START_TAG) {
					if (xpp.getName().equals("username")) {
						username = xpp.nextText();
						break;
					}
				}
				eventType = xpp.next();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return username;
	}
}
